import * as API_Distribution from '../../api/distribution'
import { seller_id,shareMessage } from "../../config/config";
Page({
  data: {
    // webSrc: 'http://127.0.0.1:3008/card/onecardindex'
    webSrc: 'https://h5.oupinguoji.cn/card/onecardindex',
    query: "",
    sharename:'',
    shareimg:'',
  },
  onLoad(options){
    API_Distribution.getsharemessage(seller_id).then( res => {
      // console.log( res) 
      this.setData({
        sharename:res.shop_name,
        shareimg:res.shop_mini_logo
      })
    }).catch( err => {
      console.error( err)
    })
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log(res);
        var latitude = res.latitude
        var longitude = res.longitude
        var location = longitude + ',' + latitude
        // Console.log('开始')
        // console.log(location)
        // console.log('结束')
        //弹框
        // wx.showModal({
        //   title: '当前位置',
        //   content: "纬度:" + latitude + ",经度:" + longitude,
        // })
      }
    })
    // getLocation: function (e) {
    //   wx.getLocation({
    //     type: 'wgs84',
    //     success: function (res) {
    //       // console.log(res);
    //       var latitude = res.latitude
    //       var longitude = res.longitude
    //       //弹框
    //       wx.showModal({
    //         title: '当前位置',
    //         content: "纬度:" + latitude + ",经度:" + longitude,
    //       })
    //     }
    //   })
    // }
             // 上下级关系获取绑定
  if(options.su){
    API_Distribution.accessShortLink({ su: options.su }).then(res => {
      wx.setStorageSync('inviteCode',res.mobile)
    })
  }
  },
  //刷新webview页面
  async onShow(){
    // let tmpUrl = this.data.webSrc;
    // this.setData({ webSrc: ''});
    // setTimeout(() => {this.setData({webSrc: tmpUrl})}, 10);
    if (wx.getStorageSync("access_token")){
    let response = await API_Distribution.generateShortLink();
    let query = response.message.substring(
      response.message.indexOf("?"),
      response.message.length
    );
    this.setData({
      query
    })
  }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
    });
    return {
      title: this.data.sharename,
      desc: this.data.sharename,
      path: '/pages/home/home' + this.data.query,
      imageUrl: this.data.shareimg
    }
  }
})
